package com.tc168.cp306.b06.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tc168.cp306.b06.MyApplication;
import com.tc168.cp306.b06.R;
import com.tc168.cp306.b06.Utils.SPUtils;
import com.tc168.cp306.b06.bean.UserAnswerbean;
import com.tc168.cp306.b06.gen.UserAnswerbeanDao;
import com.tc168.cp306.b06.iview.NextDialog;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CuoMatterActivity extends Activity implements NextDialog.OnNoticeDialogClickListener {

    @BindView(R.id.back_ig)
    ImageView backIg;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tim)
    TextView tim;
    @BindView(R.id.title_ll)
    LinearLayout titleLl;
    @BindView(R.id.yes)
    ImageView yes;
    @BindView(R.id.no)
    ImageView no;

    private UserAnswerbeanDao topicAnswerbeanDao;
    private List<UserAnswerbean> list;

    private Boolean iszhenque;
    private int num;
    private Date date;

    private NextDialog nextDialog;
    private String format;

    private int aaa;

    private String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_matter);
        ButterKnife.bind(this);
        topicAnswerbeanDao = MyApplication.getmDaoSessiontwo().getUserAnswerbeanDao();


        name = (String) SPUtils.get(this, "name", "");

        num = getIntent().getIntExtra("num", 0);




        list = topicAnswerbeanDao.queryBuilder().where(UserAnswerbeanDao.Properties.Istrue.eq(false)).list();
        aaa = num + 1;


        if (list != null) {
            tim.setText(list.get(num).getTitle());

            iszhenque = list.get(num).getYuanis();
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
        //获取当前时间
        date = new Date(System.currentTimeMillis());
        format = simpleDateFormat.format(date);
    }

    /**
     * 提示更新
     */
    private void showNoticeDialog() {
        if (null == nextDialog) {
            nextDialog = new NextDialog(this);
            //mNoticeDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            nextDialog.setCancelable(false);
            nextDialog.setClickListener(this);
        }
        nextDialog.show("");
    }

    @OnClick({R.id.back_ig, R.id.yes, R.id.no})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_ig:
                finish();
                break;
            case R.id.yes:

                if (iszhenque == true) {

                    Toast.makeText(this, "回答正确", Toast.LENGTH_SHORT).show();

                    if (aaa < list.size()) {

                        Intent a = new Intent(CuoMatterActivity.this, CuoMatterActivity.class);

                        a.putExtra("num", aaa);

                        startActivity(a);
                    } else {
                        Toast.makeText(this, "最后一道题了", Toast.LENGTH_SHORT).show();

                    }

                    finish();

                } else {
                    showNoticeDialog();
                }
                break;
            case R.id.no:

                if (iszhenque == false) {

                    Toast.makeText(this, "回答正确", Toast.LENGTH_SHORT).show();


                    if (aaa < list.size()) {

                        Intent a = new Intent(CuoMatterActivity.this, CuoMatterActivity.class);

                        a.putExtra("num", aaa);

                        startActivity(a);
                    } else {
                        Toast.makeText(this, "最后一道题了", Toast.LENGTH_SHORT).show();

                    }

                    finish();

                } else {

                    showNoticeDialog();
                }

                break;
        }
    }

    @Override
    public void onConfirmClick() {
        nextDialog.dismiss();


        Intent a = new Intent(CuoMatterActivity.this, CuoJieshiActivity.class);


        startActivity(a);
        finish();

    }
}
